package org.spring.aicloud.utils;

import org.springframework.validation.BindException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;



/**
 * @author yexiebao
 * @date 2025/5/8
 * @description：统一异常处理
 *
 * @RestControllerAdvice
 * 全局处理控制器（Controller）层的异常（相当于 @ControllerAdvice）
 * 让返回值直接序列化为 JSON 或 XML（相当于 @ResponseBody）
 */
@RestControllerAdvice
public class ExceptionAdvice {
    @ExceptionHandler(BindException.class)
    public ResponseEntity handleBindException(BindException e){
        return ResponseEntity.error(e.getBindingResult().getAllErrors().get(0).getDefaultMessage());
    }

    @ExceptionHandler(Exception.class)
    public ResponseEntity handleException(Exception e){
        return ResponseEntity.error(e.getMessage());
    }
}
